import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/25 17:27
 * @description DFS, 维持深度
 */
public class Solution2 {
    public static void main(String[] args) {

    }

    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> res = new LinkedList<>();
        dfs(root, 0, res);
        return res;
    }

    public void dfs(TreeNode node, int depth, List<List<Integer>> res) {
        if (node == null) {
            return;
        }
        if (depth == res.size()) {
            res.add(0, new ArrayList<>());
        }
        res.get(res.size() - depth - 1).add(node.val);
        dfs(node.left, depth + 1, res);
        dfs(node.right, depth + 1, res);
    }
}
